﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.UI;
using UnityEngine.UI;
using XCharts;
using Script.UI.Component;
using LitJson;
using MTUnity.Utils;
using System;

public class UI_Humidity : UIUpdateBase
{
	public HistogramGroup Panel_6H;
	public HistogramGroup Panel_12H;
	
	public LiquidChart Liquid_Humidity;
    public Text Txt_Humidity;
    public Text Txt_RefreshTime;

    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.Warehouse.Current_Humidity , UpdateCurrentHumidity },
            {JsonFildName.Warehouse.Record_Humidity , UpdateRecordHumidity },
            {JsonFildName.Warehouse.HumidityRefreshTime , UpdateRefreshTime },
        };
    }
    #endregion

    #region UpdateInterface
    public void UpdateCurrentHumidity(JsonData jsonData)
    {
        if (jsonData.IsDouble)
        {
            UpdateCurrentHumidity(JsonUtils.ToDouble(jsonData));
        }

    }

    public void UpdateRecordHumidity(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = Utils.SetData<HistogramData_Humidity>(jsonData, JsonFildName.Warehouse.Humidity);
            UpdateRecordHumidity(listData);
        }
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    private void UpdateCurrentHumidity(double dCurrentHumidity)
    {
        //blue 00bacb
        //red c23531
        //normal FF8F27
        Color color = new Color();
        if (dCurrentHumidity > GlobalValue.MaxLimitHumidity)
        {
            color.r = 0.7607843f;
            color.g = 0.2078431f;
            color.b = 0.1921569f;
            color.a = 1;
        }
        else if (dCurrentHumidity < GlobalValue.MinLimitHumidity)
        {
            color.r = 0;
            color.g = 0.7294118f;
            color.b = 0.7960784f;
            color.a = 1;
        }
        else
        {
            color.r = 1;
            color.g = 0.5607843f;
            color.b = 0.1529412f;
            color.a = 1;
        }

        Liquid_Humidity.GetVessel(0).color = color;

        Liquid_Humidity.UpdateData(0, 0, (float)dCurrentHumidity);
        Liquid_Humidity.UpdateData(1, 0, (float)dCurrentHumidity);
        Txt_Humidity.text = string.Format("{0}", dCurrentHumidity);
    }

    private void UpdateRecordHumidity(List<HistogramDataBase> listData)
    {
        Panel_6H.Refresh(listData);
        Panel_12H.Refresh(listData);
    }

    public void SetData(UI_Humidity_Data uiHumidityData)
	{
        Liquid_Humidity.UpdateData(0, 0, uiHumidityData.CurrentHumidity);
        
        List<HistogramDataBase> listData = new List<HistogramDataBase>();
        for (int i = 0; i < uiHumidityData.HumidityRecord.Count; i++)
        {
            HistogramDataBase data = new HistogramData_Humidity()
            {
                Current = uiHumidityData.HumidityRecord[i].Humidity,
                Timestamp = uiHumidityData.HumidityRecord[i].Timestamp
            };
            listData.Add(data);
        }
        Panel_6H.Refresh(listData);
        Panel_12H.Refresh(listData);
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}